clear

********************************************************************************
*** Load and Setup Data ***
* Load dates
use "./data/dates"
sort ccode date
gen year = year(date)
gen mdate = mofd(date)
format mdate %tm

* Merge in EC/TP data
merge 1:1 ccode date using "./data/yield_component_panel"
drop _merge tp3m tp2y tp5y
gen tp_10y = tp10y*100

* Merge in FOMC-Bloomberg panel
merge 1:1 ccode date using "./data/fomc_panel"
drop _merge
replace tp10y_2day = 0 if tp10y_2day == . 

* Merge in MP data
sort date
merge m:1 date using "./data/mp_data.dta"
drop _merge

* Merge in country characteristics
sort ccode
merge m:1 ccode using "./data/characteristics"
drop _merge


* Set MP measures to 0 for non-FOMC dates 
replace mps = 0 if mps == . 
replace mpu = 0 if mpu == .
replace mps_2day = 0 if mps_2day == .
replace mpu_2day = 0 if mpu_2day == .
replace mps_2day_pre = 0 if mps_2day_pre == .
replace mpu_2day_pre = 0 if mpu_2day_pre == .
replace mps_2day_post = 0 if mps_2day_post == .
replace mpu_2day_post = 0 if mpu_2day_post == .


* Collapse to Monthly frequency
sort ccode mdate
collapse (sum) tp10y_2day mps_2day mpu_2day mps mpu mpu_2day_pre mps_2day_pre mpu_2day_post mps_2day_post (mean) tp_10y yield3m yield2y yield10y advanced year, by(ccode mdate)


* Gen Interest Rate Differential (3 month yield)
gen us3mo = yield3m if ccode == 49
sort mdate
by mdate: egen us3m = max(us3mo)
sort ccode mdate
xtset ccode mdate
gen diff = us3m - yield3m


* Drop Venezuela, US, Brazil, Mexico, Taiwan, Greece
keep if ccode < 49 
drop if ccode == 5 | ccode == 28 | ccode == 45 | ccode == 15 


* Merge in TIC
merge 1:1 ccode mdate using "./data/tic_data"
drop _merge


* Set panel
xtset ccode mdate

********************

* Gen capital flow variables 
gen ln_bond = us_bond_est_pos - L.us_bond_est_pos

* Gen interest rate differential - mp interactions
gen diff_mps = diff*mps
gen diff_mpu = diff*mpu
gen diff_mps2 = diff*mps_2day
gen diff_mpu2 = diff*mpu_2day
gen diff_mps2_pre = diff*mps_2day_pre
gen diff_mpu2_pre = diff*mpu_2day_pre
gen diff_mps2_post = diff*mps_2day_post
gen diff_mpu2_post = diff*mpu_2day_post

* Create Crisis Dummy
gen crisis = 0
replace crisis = 1 if mdate >= tm(2007m12) & mdate < tm(2009m7)

* Standardize Asset Changes
rename ln_bond ln_bond_unscl

sort ccode 
by ccode: egen bond_std = sd(ln_bond_unscl)
by ccode: egen bond_std_pre = sd(ln_bond_unscl) if mdate < tm(2007m12)
by ccode: egen bond_std_post = sd(ln_bond_unscl) if mdate >= tm(2007m12)

sort ccode mdate
gen ln_bond = ln_bond_unscl/bond_std
gen ln_bond_pre = ln_bond_unscl/bond_std_pre
gen ln_bond_post = ln_bond_unscl/bond_std_post

* Label variables
label var mps_2day "MP Surprise"
label var mpu_2day "MP Uncertainty"
label var diff "idiff"
label var diff_mps2 "MPS*idiff"
label var diff_mpu2 "MPU*idiff"


********************************************************************************
*** Response of US holdings of foreign bonds to monetary shocks ***

reghdfe ln_bond diff diff_mps2 diff_mpu2 if year > 1994 & advanced == 1 & crisis == 0, absorb(ccode mdate) cluster(ccode mdate)
outreg2 using "tables\tableA8.xml", replace se bdec(3) bracket label
reghdfe ln_bond diff diff_mps2 diff_mpu2 if year > 1994 & advanced == 0 & crisis == 0, absorb(ccode mdate) cluster(ccode mdate)
outreg2 using "tables\tableA8.xml", append se bdec(3) bracket label

********************************************************************************
